package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.model.TbContentCategory;
import com.dongdongshop.model.TbContentCategoryExample;
import com.dongdongshop.service.ContentCateGoryService;
import com.dongdongshop.vo.ContentCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentCateGoryServiceImpl implements ContentCateGoryService {
    @Resource
    private TbContentCategoryMapper contentCategoryMapper;

    @Override
    public List<ContentCategoryVO> getAllContentCateGory() {
        List<TbContentCategory> tbContentCategories = contentCategoryMapper.selectByExample(null);
        List<ContentCategoryVO> collect = tbContentCategories.stream().map(cateGory -> {
            ContentCategoryVO contentCategoryVO = new ContentCategoryVO();
            BeanUtils.copyProperties(cateGory, contentCategoryVO);
            return contentCategoryVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int addContentCateGory(ContentCategoryVO contentCategoryVO) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        BeanUtils.copyProperties(contentCategoryVO,tbContentCategory);
        int i = contentCategoryMapper.insertSelective(tbContentCategory);
        return i;
    }

    @Override
    public ContentCategoryVO getContentCateGoryById(Long id) {
        TbContentCategory tbContentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        ContentCategoryVO contentCategoryVO = new ContentCategoryVO();
        BeanUtils.copyProperties(tbContentCategory, contentCategoryVO);
        return contentCategoryVO;
    }

    @Override
    public int updateContentCateGory(ContentCategoryVO contentCategoryVO) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        BeanUtils.copyProperties(contentCategoryVO,tbContentCategory);
        int i = contentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
        return i;
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        int i = contentCategoryMapper.deleteByExample(example);
        return i;
    }
}
